/* -----------------
* brief
* 1. this is code gen by tools
* 2. array like [a,b,c] after each field, define the ui html tag in view,
		a for title,
		b for show or not ,
		c for element html tag , currently support : check、radio、select、input、textarea、upload
* 3. XXX
*/
//使用方法：在工程root目录下，执行go generate ./modules/wechat/models
//go:generate legogen -type=WxCardMch -kv=ModuleName=wechat,PrjName=saas_sys_base -dirconf=../../.. ./WxCardMch.go
package wechat

import (
	wxcardmch "github.com/chanxuehong/wechat/mp/card/submerchant"
	"github.com/mjiulee/lego"
)

func init() {
	lego.AddBeanToSynList(new(WxCardMch))
}

/* table: "tb_wx_card_mch"
 * -----------------
 */
type WxCardMch struct {
	Id    int64  `xorm:"pk" json:",string"`               // ["id","hide","hidden"]
	IfDel int    `xorm:"default 0"`        // ["deleted","hide","hidden"]
	Cdate string `xorm:"DateTime created"` // ["create","show","datetime"]
	Udate string `xorm:"DateTime updated"` // ["update","show","datetime"]
	// add your custom field here
	//所属信息
	TenantId  int64  `xorm:"unique" json:",string"`                            // ["TenantId","show","input"]
	CardMchId int64  `xorm:"unique(grp_uqe_mchid)" json:",string"`             // ["CardMchId","show","input"] //子商户id，对于一个母商户公众号下唯一，所以要加上下面的weappid字段标识
	Weappid   string `xorm:"unique(grp_uqe_mchid) varchar(24)"` // ["Weappid","show","input"]

	//业务字段
	/*这个appid和上面的weappid的区别是，
	下面的appid是接口配置这个商户在微信端关联的公众号id
	而上面weappid指，我们服务端端关联的公众号id
	*/
	AppId     string `xorm:"varchar(24)"` // ["AppId","show","input"] //目前的场景不使用此字段
	BrandName string `xorm:"char(12)"`    // ["BrandName","show","input"]
	LogoUrl   string `xorm:""`            // ["LogoUrl","show","upload"]
	Status    string `xorm:""`            // ["Status","show","select"]
	Reason    string `xorm:""`            // ["Reason","show","input"]
	//资质材料保存位置，主要有3个资质材料：1. 子商户授权函、2营业执照、3. 营业执照登记者身份证照
	//ProtocolMediaId string `json:"protocol"`
	ProtocolUrl  string `xorm:""` // ["ProtocolUrl","show","upload"]
	AgreementUrl string `xorm:""` // ["AgreementUrl","show","upload"]
	OperatorUrl  string `xorm:""` // ["OperatorUrl","show","upload"]

	//所属类目
	PrimaryCategoryId   int `xorm:""` // ["PrimaryCategoryId","show","select"]
	SecondaryCategoryId int `xorm:""` // ["SecondaryCategoryId","show","select"]

	//时间
	CreateTimeMch int64 `xorm:"" json:",string"` // ["协议开始时间","show","select"]
	UpdateTimeMch int64 `xorm:"" json:",string"` // ["子商户信息更新时间","show","select"]
	BeginTimeMch  int64 `xorm:"" json:",string"` // ["创建时间","show","select"]
	EndTimeMch    int64 `xorm:"" json:",string"` // ["授权函有效期截止时间","show","select"]
}

type CardMchInfoDto struct {
	AppId     string
	BrandName string
	LogoUrl   string
	//Status    string
	//Reason    string

	//资质材料保存位置，主要有3个资质材料：1. 子商户授权函、2营业执照、3. 营业执照登记者身份证照
	//ProtocolMediaId string `json:"protocol"`
	//ProtocolId       string
	ProtocolUrl string
	//AgreementMediaId string
	AgreementUrl string
	//OperatorMediaId  string
	OperatorUrl string

	PrimaryCategoryId   int
	SecondaryCategoryId int

	EndTime int64
}

func NewWxCardMch(TenantId int64, weappid string, mchId int64, para *CardMchInfoDto) *WxCardMch {
	rc := new(WxCardMch)
	rc.Status = wxcardmch.SUBMCH_STATUS_CHECKING
	rc.TenantId = TenantId
	rc.Weappid = weappid
	rc.CardMchId = mchId
	rc.Set(para)

	return rc
}
func (t *WxCardMch) Set(para *CardMchInfoDto) {
	t.Status = para.AppId

	t.BrandName = para.BrandName
	t.LogoUrl = para.LogoUrl
	//Status    string
	//Reason    string

	//资质材料保存位置，主要有3个资质材料：1. 子商户授权函、2营业执照、3. 营业执照登记者身份证照
	//ProtocolMediaId string `json:"protocol"`
	//t.Protocol = para.ProtocolId
	t.ProtocolUrl = para.ProtocolUrl
	//t. = para.AgreementId
	t.AgreementUrl = para.AgreementUrl
	//t. = para.OperatorId
	t.OperatorUrl = para.OperatorUrl

	t.PrimaryCategoryId = para.PrimaryCategoryId
	t.SecondaryCategoryId = para.SecondaryCategoryId
	t.EndTimeMch = para.EndTime
}

func (t *WxCardMch) SyncFromResp(resp *wxcardmch.SubmitSubmerchantResp) {
	t.CardMchId = resp.Info.MerchantId
	t.AppId = resp.Info.AppId
	t.BrandName = resp.Info.BrandName
	t.LogoUrl = resp.Info.LogoUrl
	t.Status = resp.Info.Status

	t.CreateTimeMch = resp.Info.CreateTime
	t.UpdateTimeMch = resp.Info.UpdateTime
	t.BeginTimeMch = resp.Info.BeginTime
	t.EndTimeMch = resp.Info.EndTime

	t.PrimaryCategoryId = resp.Info.PrimaryCategoryId
	t.SecondaryCategoryId = resp.Info.SecondaryCategoryId

}
